package com.tea.controller;

import com.tea.Model.GoodsSpec;
import com.tea.Model.dto.BatchSpecStockUpdateDTO;
import com.tea.Model.dto.SpecStockUpdateDTO;
import com.tea.service.GoodsSpecService;
import com.tea.utils.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;

/**
 * 商品规格管理
 */
@RestController
@RequestMapping("/goodsSpec")
@Slf4j
public class GoodsSpecController {

    @Resource
    private GoodsSpecService goodsSpecService;

    /**
     * 获取规格库存信息
     */
    @GetMapping("/stock/{specId}")
    public Result<GoodsSpec> getSpecStock(@PathVariable Long specId) {
        return goodsSpecService.getSpecStock(specId);
    }

    /**
     * 更新单个规格库存
     */
    @PostMapping("/stock/update")
    public Result<Object> updateSpecStock(@RequestBody @Valid SpecStockUpdateDTO dto) {
        try { log.info("更新规格库存，请求：specId={}, operation={}, quantity={}", dto.getSpecId(), dto.getOperation(), dto.getQuantity()); } catch (Exception ignore) {}
        return goodsSpecService.updateSpecStock(dto);
    }

    /**
     * 批量更新规格库存
     */
    @PostMapping("/stock/batchUpdate")
    public Result<Object> batchUpdateSpecStock(@RequestBody @Valid BatchSpecStockUpdateDTO dto) {
        try { log.info("批量更新规格库存，请求：itemsCount={}", dto.getItems() != null ? dto.getItems().size() : 0); } catch (Exception ignore) {}
        return goodsSpecService.batchUpdateSpecStock(dto);
    }
}